package com.lanhai.fnstore.dispatch;
 

import com.lanhai.fnstore.Tools;
import com.lanhai.fnstore.cache.AgvCache;
import com.lanhai.fnstore.cache.AgvItem;
import com.lanhai.fnstore.cache.EstimateCache; 
import com.lanhai.fnstore.cache.MountCache;
import com.lanhai.fnstore.cache.ParkCache;
import com.lanhai.fnstore.cache.ParkItem;
import com.lanhai.fnstore.cache.ParkRelationCache;
import com.lanhai.fnstore.cache.PowerCache; 
import com.lanhai.fnstore.cache.RoadCache;
import com.lanhai.fnstore.cache.StationGroupCache;
import com.lanhai.fnstore.cache.TranfficAreaCache;
import com.lanhai.fnstore.queue.DispatchUnit;

/**
 * 内部调度处理
 * @author virus408
 *
 */
public class DispatchResolve {

	public static void Resolve(DispatchUnit unit) {
		switch(unit.getTarget()) {
		case Judgement:
			//传输调度
			ParkItem park=ParkCache.getParkByNo(unit.getParkNo()); 
			if(park==null) {
				return;
			} 
			MountCache.judgement(AgvCache.getAgvIdByPark(unit.getParkNo()), park);
			break;
		
		case SelectRoad:
			//路线选择,目前不存在
			break;
		case Hacker:
			//响应后台指令 
			byte[] bf=unit.getData();
			if(bf.length>4) {
				 //999= E7 03 00 00
				DoHack(bf);  
			} 
			 break;
		case NoticeDecide:
			////释放了对接设备 
			//通知对应的装卸分配点有空机台可用
			//本程序没有设计待命点，不需要做什么
			break;
		case NoticeEstimate:
			////通知预判断点放行小车
			int estimateNo=ParkRelationCache.getEstimateParkNo(unit.getParkNo());
			 if(estimateNo<=0);
			int agvid=	EstimateCache.nextAgv(estimateNo);
			if(agvid==0) {
				//没有野车
				return;
			}
			//让该小车进入装卸判断位
			AgvItem agv2=AgvCache.getAgvByID(agvid);
			if(agv2!=null) {
				int road=ParkCache.getTargetRoad(estimateNo);
				if(road>0) {
					 RoadCache.lockRoad(road, true); //加锁，防止后面的小车进来
				}
				AgvResolve.AgvToRoad(agv2, road); 
			}
			
			break;
		case Nono:
			break;
		default:
			break;
		
		}
	}
	
	
	static void DoHack(byte[] data) {
		//第1位是指令， 之后四位是编号 
		 /*
	     启用对接设备=1,
       停用对接设备=2,
       释放交管=3,
       释放小车=4,
       启用设备分组=5,
       停用设备分组=6,
        停用充电桩=7,
        启用充电桩=8,
		  */
		int id=Tools.toIntL(new byte[] {data[1],data[2],data[3],data[4]}); 
		switch(data[0]) {
		case 1:
			//启用对接设备
			ParkItem pk1=ParkCache.getParkByNo(id);
			if(pk1==null) return;
			StationGroupCache.updateDisable(pk1.getStationGroup(), id, false); 
			break;
		case 2:
			// 停用对接设备
			ParkItem pk2=ParkCache.getParkByNo(id);
			if(pk2==null) return;
			StationGroupCache.updateDisable( pk2.getStationGroup(),  id, true); 
			break;
		case 3:
			// 释放交管
			TranfficAreaCache.releaseArea(id); 
			break;
		case 4:
			// 释放小车
			AgvCache.clearSource(id);
			break;
		case 5:
			//启用设备分组
			StationGroupCache.updateDisable(   id, false); 
			break;
		case 6:
			//停用设备分组
			StationGroupCache.updateDisable(   id, true); 
			break;
		case 7:
			// 停用充电桩
			ParkItem pk7=ParkCache.getParkByNo(id);
			if(pk7!=null) {
				PowerCache.updateDisable(id, pk7.getParkName(),true);
			}
			
			break;
		case 8:
			//启用充电桩
			ParkItem pk8=ParkCache.getParkByNo(id);
			if(pk8!=null) {
				PowerCache.updateDisable(id,pk8.getParkName(), false);
			}
			break;
			default:
				return;
		}
	}
}
